<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button class="begin">开始</button>
    <button class="stop">停止</button>
    <script>
        var begin = document.querySelector('.begin')
        var stop = document.querySelector('.stop')
        // 停止定时器需要该定时器标识符 然而是不同的函数域不能使用各自的局部变量 所以设置一个为null 的全局变量再去赋值和调用 
        var ks = null
        begin.addEventListener('click', function () {
            ks = setInterval(function () {
                console.log('开始');
            }, 1000)
        })
        stop.addEventListener('click', function () {
            clearInterval(ks)
        })


    </script>
</body>

</html>